import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class Testkey {
    //发送http请求
    public static String load(String url, String query) throws Exception
    {
        URL restURL = new URL(url);
        /*
         * 此处的urlConnection对象实际上是根据URL的请求协议(此处是http)生成的URLConnection类 的子类HttpURLConnection
         */
        HttpURLConnection conn = (HttpURLConnection) restURL.openConnection();
        //请求方式
        conn.setRequestMethod("POST");
        //设置是否从httpUrlConnection读入，默认情况下是true; httpUrlConnection.setDoInput(true);
        conn.setDoOutput(true);
        //allowUserInteraction 如果为 true，则在允许用户交互（例如弹出一个验证对话框）的上下文中对此 URL 进行检查。
        conn.setAllowUserInteraction(false);
        PrintStream ps = new PrintStream(conn.getOutputStream());
        ps.print(query);
        ps.close();
        BufferedReader bReader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line,resultStr="";
        while(null != (line=bReader.readLine()))
        {
            resultStr +=line;
        }
        System.out.println("3412412---"+resultStr);
        bReader.close();
        return resultStr;
    }
//生成随机数字字母组合
public  static  String getCharAndNumr(int length)
{
    String val = "";
    Random random = new Random();
    for(int i = 0; i < length; i++)
    {
        String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num"; // 输出字母还是数字

        if("char".equalsIgnoreCase(charOrNum)) // 字符串
        {
            int choice =65; //取得大写字母还是小写字母
            val += (char) (choice + random.nextInt(26));
        }
        else if("num".equalsIgnoreCase(charOrNum)) // 数字
        {
            val += String.valueOf(random.nextInt(10));
        }
    }
    return val;
}
    public static void main(String []args) {try {
        //加密升级接口
        String reg="http://222.143.25.133:7777/encryptionMachine/encryptionMachineBusiness/";
        //终端管理
        String str="http://222.143.25.133:7777/device/deviceaction/getActionData";
        //本机MAC
    //随机数
        String num=getCharAndNumr(16);
    //获取时间
        System.currentTimeMillis();
        System.out.println( System.currentTimeMillis());
        Testkey restUtil = new Testkey();
String query="params=<DEVICEINFO>\n" +
        "8649M11D150718000087000000000000|WD00031980|8649M11D150718000087000000000000|00000000000000000000<HEADER>V1.0|42010000120150617070120001|420100001|420000000|420100001|42010000120150617070120001|028|1|20140916105542|20140916|105542|0<MAC>220100|00814A444686602201001C940C|03|220100D156000005000000018DABB60C|"+num+"|"+num+"|"+num+"|"+num+"<CAINFO>BYQERMUF|420100001|410|RDgsLEBYQERMUFRUVDA8XGBYUGB<END>\n" +
        "+'\r\n\'";
        String resultString = load(
                reg+"cardExternalAuth",
                query);
    } catch (Exception e) {
        // TODO: handle exception
        System.out.print(e.getMessage());
    }
    }
}